import { is } from '@electron-toolkit/utils'
import { join } from 'path'
import { BrowserWindow, LoadFileOptions } from 'electron'

export function getBaseUrl(): string {
  if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
    return process.env['ELECTRON_RENDERER_URL']
  } else {
    return join(__dirname, '../renderer/index.html')
  }
}

export function load(win: BrowserWindow, options?: LoadFileOptions): void {
  const url = getBaseUrl()

  if (is.dev) {
    console.log('url', url + (options?.hash ?? ''))
    win.loadURL(url + (options?.hash ?? ''))
  } else {
    console.log('load url', getBaseUrl())
    win.loadFile(getBaseUrl(), options)
  }
}
